/*
 * @Author: ChunLai
 * @Date: 2021-11-22 09:08:09
 * @LastEditTime: 2022-01-10 17:47:02
 * @Description: 防抖函数
 * @FilePath: \1.new_projects\src\utils\debounce.js
 */
import {
  Toast
} from 'vant';
/**
 * @description: 防抖函数
 * @param {需要防抖的函数} func
 * @param {延迟时间} waitTime
 * @param {是否立即执行} immediate
 */
export function debounce(func, waitTime, immediate) {
  let timeout;
  let wait = waitTime || 1000;
  return function (...args) {
    let context = this;
    if (timeout) clearTimeout(timeout);
    if (immediate) {
      let callNow = !timeout;
      timeout = setTimeout(function () {
        timeout = null;
      }, wait);
      if (callNow) {
        func.apply(context, args)
      } else {
        Toast('请勿频繁点击');
      }
    } else {
      timeout = setTimeout(function () {
        func.apply(context, args);
      }, wait);
    }
  };
}